//
//  HProductController.swift
//  StarTimesON
//
//  Created by Toj on 7/18/23.
//  Copyright © 2023 StarTimes. All rights reserved.
//

import UIKit

protocol HProductControllerDelegate: NSObjectProtocol {
    func productControllerDidTapClosed(_ vc: HProductController)
}

// MARK: - 懒加载(属性), 变量, 初始化init
class HProductController: UIViewController {
    
    weak var pdelegate: HProductControllerDelegate?
    
    var admobModel: STAdmobModel?
    
    /* headerView */
    private lazy var headerView: HProductContentHeaderView = {
        let view = HProductContentHeaderView()
        view.delegate = self
        return view
    }()
    
    /* collectionView */
    private(set) lazy var mobileViewController: ContentModeViewController = {
        let vc = ContentModeViewController()
        vc.topInset = 0
        return vc
    }()
    
    /* collectionView */
    private(set) lazy var tvViewController: STTableViewController = {
        let vc = STTableViewController()
        vc.topInset = 0
        return vc
    }()
    
    /* contentView */
    private lazy var contentView: UIView = {
        let view = UIView()
        view.isHidden = true
        return view
    }()
}

// MARK: - 生命周期
extension HProductController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
}

// MARK: - 点击事件
@objc private extension HProductController {
    
}

// MARK: - Public
extension HProductController {
    
    func showAnimate(admobModel: STAdmobModel? = nil, selectedTV: Bool) {

        guard headerView.admobModel == nil else { return }
        // 如果商隐: 肯定商显/ 商广显(广告有值) 动画
        guard contentView.isHidden else {
            // 如果商品显: 可能是广显(广显: 广告有值)
            if admobModel == nil {
                headerView.selectedTV = selectedTV
                return
            }
            // 广显 动画
            showAdAnimate(admobModel: admobModel)
            return
        }
        
        view.isHidden = false
        contentView.isHidden = false
        headerView.selectedTV = selectedTV
        view.layoutIfNeeded()
        showProductAndAd(admobModel: admobModel)
    }
}

// MARK: - Private
private extension HProductController {
    
    func headerViewHeight(admobModel: STAdmobModel? = nil) -> CGFloat {
        return admobModel == nil ? 48 : 168 // 120 + 48
    }
    
    func reloadHeaderView(admobModel: STAdmobModel? = nil) {
        var headerFrame = headerView.frame
        let headerHeight = headerViewHeight(admobModel: admobModel)
        headerFrame.size.height = headerHeight
        headerView.frame = headerFrame
    }
    func showAdAnimate(admobModel: STAdmobModel? = nil) {
        reloadHeaderView(admobModel: admobModel)
        headerView.show(admobModel: admobModel, rewardView: true)
    }
    
    func showProductAndAd(admobModel: STAdmobModel? = nil) {
        self.admobModel = admobModel
        
        headerView.show(admobModel: admobModel)
        reloadHeaderView(admobModel: admobModel)
        
        contentView.snp.remakeConstraints { make in
            make.top.equalTo(view.snp.bottom)
            make.left.right.height.equalTo(view)
        }
        
        view.layoutIfNeeded()
        
        contentView.snp.remakeConstraints { make in
            make.edges.equalTo(view)
        }
        
        UIView.animate(withDuration: 0.25) {
            self.view.layoutIfNeeded()
        }
    }
    
    func closeViews() {
        headerView.hiddenReward()
        view.isHidden = true
        contentView.isHidden = true
        pdelegate?.productControllerDidTapClosed(self)
    }
}

// MARK: - 代理: HProductContentHeaderViewDelegate
extension HProductController: HProductContentHeaderViewDelegate {
    
    func headerViewDidTapClose(_ headerView: HProductContentHeaderView) {
        
        let contentHeight = contentView.frame.height
        contentView.snp.remakeConstraints { make in
            make.height.equalTo(contentHeight)
            make.left.right.equalTo(view)
            make.top.equalTo(view.snp.bottom)
        }
        
        UIView.animate(withDuration: 0.25) {
            self.view.layoutIfNeeded()
        } completion: { _ in
            self.closeViews()
        }
    }
    
    func headerViewDidTapGetVIP(_ headerView: HProductContentHeaderView) {
        let vc = STOSRViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    func headerViewDidTapSignin(_ headerView: HProductContentHeaderView) {
        STSignInViewController.shared.presentController()
    }
    
    func headerViewDidTapMobile(_ headerView: HProductContentHeaderView) {
        mobileViewController.view.isHidden = false
        tvViewController.view.isHidden = true
    }
    
    func headerViewDidTapTV(_ headerView: HProductContentHeaderView) {
        mobileViewController.view.isHidden = true
        tvViewController.view.isHidden = false
    }
}

// MARK: - 初始化设置
private extension HProductController {
    
    func setupMakeAddSubviews() {
        view.addSubview(contentView)
        
        addChild(mobileViewController)
        contentView.addSubview(mobileViewController.view)
        
        addChild(tvViewController)
        contentView.addSubview(tvViewController.view)
        
        contentView.addSubview(headerView)
    }
    
    func setupMakeLayoutSubviews() {
        
        contentView.snp.remakeConstraints { make in
            make.top.equalTo(view.snp.bottom)
            make.left.right.height.equalTo(view)
        }
        
        view.layoutIfNeeded()
        
        let headerHeight = headerViewHeight()
        let viewWidth = view.frame.width
        headerView.frame = CGRect(x: 0, y: 0, width: viewWidth, height: headerHeight)
        
        mobileViewController.view.snp.remakeConstraints { make in
            make.left.right.bottom.equalTo(contentView)
            make.top.equalTo(headerView.snp.bottom)
        }
        
        tvViewController.view.snp.remakeConstraints { make in
            make.edges.equalTo(mobileViewController.view)
        }
    }
    
    func setupMakeInitialize() {
        
    }
}
